package Controller

import (
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"hr-system/Global"
	"hr-system/Model"
	response "hr-system/Response"
	"strconv"
)

type IPostManageController interface {
	RestController
	Update(c *gin.Context)
	Delete(c *gin.Context)
}

type PostManageController struct {
	DB *gorm.DB
}

func NewPostManageController() IPostManageController{
	db := Global.GetDB()
	return PostManageController{DB: db}
}

func (d PostManageController) PageList(c *gin.Context) {
	idStr, _ := c.GetQuery("id")
	id, _ := strconv.Atoi(idStr)
	if id != 0 {
		var selectStore Model.Employee

		// 查询
		err := Global.DB.Model(&selectStore).Where("id=?", id).First(&selectStore).Error
		if err != nil {
			response.Fail(c, err.Error(), nil)
			return
		}
		response.Success(c, gin.H{"store": selectStore}, "查询成功")
		return
	}
	// 获取分页参数
	tempNum, _ := c.GetQuery("pagenum")
	tempSize, _ := c.GetQuery("pagesize")
	pageNum, _ := strconv.Atoi(tempNum)   // 当前页数
	pageSize, _ := strconv.Atoi(tempSize) // 页内个数
	if pageNum == 0 || pageSize == 0 {
		pageNum = 1
		pageSize = 10
	}
	// 分页
	var stores []Model.Employee
	d.DB.Order("id").Offset((pageNum - 1) * pageSize).Limit(pageSize).Find(&stores)

	// 前端渲染分页也要知道总数
	var total int64
	d.DB.Model(Model.Employee{}).Count(&total)

	response.Success(c, gin.H{"data": stores, "total": total}, "查询成功")
}

func (d PostManageController) Create(c *gin.Context) {
	panic("implement me")
}

func (d PostManageController) Update(c *gin.Context) {
	panic("implement me")
}

func (d PostManageController) Delete(c *gin.Context) {
	panic("implement me")
}
